﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Windows.Forms;

namespace CalemShop
{
    public class GMail
    {
        private GMail()
        {
        }

        public static bool sendMail(string mailTo, string subject, string mailMessage)
        {
            try
            {
                SmtpClient smtp = new SmtpClient();
                MailMessage mail = new MailMessage();
                mail.From = new MailAddress("vinhan21@gmail.com");
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;   // [1] You can try with 465 also, I always used 587 and got success
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this
                smtp.UseDefaultCredentials = false; // [3] Changed this
                smtp.Credentials = new NetworkCredential(mail.From.Address, "vinhan123");  // [4] Added this. Note, first parameter is NOT string.
                //recipient address
                mail.To.Add(new MailAddress(mailTo));
                //Formatted mail body
                mail.Subject = subject;
                mail.BodyEncoding = Encoding.UTF8;
                mail.IsBodyHtml = true;
                mail.Body = mailMessage;
                smtp.Send(mail);
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.StackTrace);
            }

            return false;
        }

        public static bool sendMail(string[] mailTo, string subject, string mailMessage)
        {
            try
            {
                SmtpClient smtp = new SmtpClient();
                MailMessage mail = new MailMessage();
                mail.From = new MailAddress("vinhan21@gmail.com");
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;   // [1] You can try with 465 also, I always used 587 and got success
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this
                smtp.UseDefaultCredentials = false; // [3] Changed this
                smtp.Credentials = new NetworkCredential(mail.From.Address, "vinhan123");  // [4] Added this. Note, first parameter is NOT string.
                //recipient address
                foreach (string addr in mailTo)
                {
                    mail.To.Add(new MailAddress(addr));
                }
                //Formatted mail body
                mail.Subject = subject;
                mail.BodyEncoding = Encoding.UTF8;
                mail.IsBodyHtml = true;
                mail.Body = mailMessage;
                smtp.Send(mail);
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.StackTrace);
            }

            return false;
        }
    }
}
